package com.lancoo.klgcourseware.ui.widget;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

/**
 * Created by pengmi on 2020/5/7 17:09
 * Description:
 */
public class CustomNestedScrollView extends NestedScrollView {

    private OnScrollListener onScrollListener;
    public CustomNestedScrollView(@NonNull Context context) {
        super(context);
    }

    public CustomNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private boolean isScrollHide,isStartMove,isUnLink,hasNoteTool=true;
    private float startX,startY;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.e("aaaa","event:"+event.getAction()+"...x:"+event.getX()+"....y:"+event.getY());
//        if(isScrollHide){
//            return super.dispatchTouchEvent(event);
//        }
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                isStartMove=true;
                isUnLink=true;
                isScrollHide=false;
                startX=event.getX();
                startY=event.getY();
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
//                break;
                return true;
            case MotionEvent.ACTION_MOVE:
                if(Math.abs(startX-event.getX())*0.75<Math.abs(startY-event.getY())){
                    if(isStartMove){
                        isStartMove=false;
                        if(onScrollListener!=null){
                            onScrollListener.startOnTouch();
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                Log.e("ObservableX5WebView","MotionEvent.ACTION_UP");
                isUnLink=false;
                if(onScrollListener!=null){
                    onScrollListener.endOnTouch();
                }
            case MotionEvent.ACTION_CANCEL:
                isUnLink=false;
//                Log.e("ObservableX5WebView","MotionEvent.ACTION_CANCEL");
                if(onScrollListener!=null){
                    onScrollListener.endOnTouch();
                }
                break;
        }
        return super.dispatchTouchEvent(event);
    }


    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        // webview的高度
//        float webcontent = getContentHeight() * getScale();
        // 当前webview的高度
//        Log.e("aaaaa","webcontent:"+webcontent+",t:"+t+",oldt:"+oldt);
//        Log.e("aaaaa","webcontent:"+webcontent+",l:"+l+",oldl:"+oldl);
//        float webnow = getHeight() + getScrollY();
        if(t-oldt==0){
            return;
        }
        if(isScrollHide){
            return;
        }
        if(isUnLink){
            return;
        }
        if(onScrollListener!=null){
            onScrollListener.onScrollChanged(l, t, oldl, oldt);
        }

    }

    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }
}
